<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>08_计算机练习</title>
    </head>
    <body>
        <input type="text" id="i1" placeholder="请输入数字">
        <br>
        <input type="text" id="i2" placeholder="请输入数字">
        <br>
        <button onclick="f('+')" >加</button>
        <button onclick="f('-')" >减</button>
        <button onclick="f('*')" >乘</button>
        <button onclick="f('/')" >除</button>
        <hr>
        <h4>运算结果：<span></span></h4>
        <script>
            function f(o){
                let i1 = document.getElementById('i1').value;
                let i2 = document.getElementById('i2').value;
                a = parseFloat(i1);
                b = parseFloat(i2);
                let spanE = document.querySelector('span');
                switch(o){
                    case "+":
                        spanE.innerHTML = a+b;
                        break;
                    case "-":
                        spanE.innerHTML = a-b;
                        break;
                    case "*":
                        spanE.innerHTML = a*b;
                        break;
                    case "/":
                        if(b==0){
                            alert("除数不能为0");
                        }
                        spanE.innerHTML = a/b;
                        break;
                }
            }
        </script>
    </body>
</html>